<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import router from "@/router/router";
import {getToken} from "@/util/auth";

export default {
  name: "app",
  data() {
    return {};
  },
  watch: {},
  created() {
    this.setTimer()
  },
  methods: {
    setTimer() {
      if(this.timer == null) {
        this.timer = setInterval( () => {
          const accessToken = getToken()
          // 没有token跳转首页
          if (!accessToken) {
            this.$store.dispatch('FedLogOut').then(() => router.push({path: '/login'}))
            return
          }

          const accessTokenTokenExp = JSON.parse(decodeURIComponent(escape(window.atob(accessToken.split('.')[1])))).exp
          const nowTimestamp = Date.parse(new Date()) / 1000
          console.log(accessTokenTokenExp - nowTimestamp)

          this.$store.dispatch("RefreshToken")


          // if (accessTokenTokenExp - nowTimestamp <= 0) {
          //   store.dispatch('FedLogOut').then(() => router.push({path: '/login'}))
          // } else if (accessTokenTokenExp - nowTimestamp > 10 && accessTokenTokenExp - nowTimestamp < 60) {
          //   this.$store.dispatch("RefreshToken").then((res) => {
          //     //..
          //   }).catch((e) => {
          //     console.log(e)
          //   });
          // }
        }, 30000)
      }
    }
  },
  computed: {}
};
</script>
<style lang="scss">
#app {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
